<?php
/**
 * 《我的试卷》实现类
 *
 * @author 孙晓晔
 * @version $Id: Work.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/../exam/ExamEmp.class.php');

class MyExam extends ExamEmp {

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}
	
	function my($controller, $request) {
		$this->pretreat($controller);
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a WHERE emp = '%s'";
		$sql = sprintf($fmt, $this->tbl, $_SESSION['id']);
		$result = $db->query($sql);
		
		while ($row = $db->fetch_array($result)) {			
			$row['exam_t'] = $this->exam_t($row['exam']);
			$row['status_t'] = $this->status[$row['status']];
			
			$data[] = $row;
		}
		
		$controller->assign_by_ref('result', $data);

		$controller->display($request, 'my/exam/my');
		return true;
	}
		
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);
		
		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);
		
		$controller->assign_by_ref('result', $row);

		if ($row['status'] == '1') {
			$db = newdb($this->dsn_w);
			$fmt = "UPDATE %s SET status = '2' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $id);
			$db->query($sql);
		}
		
		$this->_question($controller, $row['exam'], $row['emp']);		

		$controller->display($request, 'my/exam/edit');
		return true;
	}
			
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		debug(print_r($request, true));
		
		$id = $request['id'];
		$exam = $request['exam'];
		$status = $request['status'];
		
		$emp = $_SESSION['id'];
		
		if ($status == '3') {
			$db = newdb($this->dsn_w);
			$fmt = "UPDATE %s SET status = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $status, $id);
			debug($sql);
			$db->query($sql);
		}
				
		while (list($k, $v) = each($request)) {
			if (substr($k, 0, 3) == "cb_") {
				$id = substr($k, 3);
				$data = implode('', $v);
				$this->_answer($exam, $emp, $id, $data);
				// debug($id . ", " . $data);
			}
		}
		
		$answer = $request['answer'];
		while (list($k, $v) = each($answer)) {			
			$this->_answer($exam, $emp, $v['name'], $v['value']);	
		}
		
		// debug(print_r($answer, true));
		$this->my($controller, $request);
		return true;
	}
	
	function _answer($exam, $emp, $question, $answer) {
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE exam = '%s' AND emp = '%s' AND question = '%s'";
		$sql = sprintf($fmt, Constant::tbl_exam_answer, $exam, $emp, $question);
		// debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			$fmt = "UPDATE %s SET answer = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, Constant::tbl_exam_answer, $answer, $row['id']);
		} else {
			$fmt = "INSERT INTO %s(exam, emp, question, answer, createtime) VALUES('%s', '%s', '%s', '%s', now())";
			$sql = sprintf($fmt, Constant::tbl_exam_answer, $exam, $emp, $question, $answer);
		}
		debug($sql);
		$db->query($sql);
	}	
	
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);
		$row = $this->id($this->tbl, $id);
		
		$controller->assign_by_ref('result', $row);
				
		$this->_question($controller, $row['exam'], $row['emp']);		

		$controller->display($request, 'my/exam/view');
	}
}
?>